<template>
	<view class="page-box">
		<view class="order" v-for="(item, index) in orderList" :key="item.goodsId">
			<view class="top">
				<view class="left">
					<!-- <u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon> -->
					<view class="store">{{ item.title }}</view>
					<!-- <u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon> -->
				</view>
				<view v-if="item.sellStatus == '1'" class="right">已寻回</view>
				<view v-else class="right">未寻回</view>
			</view>
			<view class="item">
				<view class="left">
					<image v-if="item.image" :src="item.image.split(',')[0]" mode="aspectFill"></image>
				</view>
				<view class="content">
					<view class="title u-line-2">{{ item.introduce }}</view>
					<view class="type">{{ item.address }}</view>
					<view class="delivery-time">发布时间 {{ item.createTime }}</view>
				</view>
			</view>
			<view class="bottom">
				<view @click="lookBtn(item)" class="logistics btn">查看</view>
				<view @click="findBtn(item)" class="logistics btn">寻回</view>
				<view @click="editBtn(item)" class="evaluate btn">编辑</view>
				<view @click="deleteBtn(item)" class="evaluate btn">删除</view>
			</view>
		</view>
		<u-loadmore @loadmore="loadmore" :status="loadStatus" bgColor="#f2f2f2"></u-loadmore>
		<!-- 删除弹框 -->
		<u-popup :mask-close-able="false" border-radius="15" v-model="show" mode="center" width="70%" height="120px">
			<view style="padding: 20px 15px 20px 15px;">
				确定删除该数据吗？
			</view>
			<view class="conBtn">
				<u-button @click="confirmBtn" :custom-style="confirmCancelBtn" style="margin-right: 10px;" type="success">确实</u-button>
				<u-button  @click="cancelBtn" :custom-style="customCancelBtn" style="margin-left: 10px;" type="error">取消</u-button>
			</view>
		</u-popup>
		<!-- 寻回弹框 -->
		<u-popup :mask-close-able="false" border-radius="15" v-model="findshow" mode="center" width="70%" height="120px">
			<view style="padding: 20px 15px 20px 15px;">
				确定物品已经寻回吗？
			</view>
			<view class="conBtn">
				<u-button @click="findconfirmBtn" :custom-style="confirmCancelBtn" style="margin-right: 10px;" type="success">确实</u-button>
				<u-button  @click="findcancelBtn" :custom-style="customCancelBtn" style="margin-left: 10px;" type="error">取消</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
	import {
		onReachBottom,
		onReady
	} from '@dcloudio/uni-app';
	import {
		reactive,
		ref
	} from 'vue';
	const show = ref(false)
	const findshow = ref(false)
	import {
		getMyFindOrClaimApi
	} from '../../src/api/user.js'
	import {deleteApi,hasClaimandFindApi} from '../../src/api/goods.js'
	const loadStatus = ref('loadmore')
	//列表数据
	const orderList = ref([])
	//总页数
	const pages = ref(0)
	const customCancelBtn =reactive({
		background:'#FF7670'
	})
	const confirmCancelBtn =reactive({
		background:'#4BC26A'
	})
	//列表参数
	const parm = reactive({
		currentPage: 1,
		pageSize: 10,
		type: "0",
		userId: uni.getStorageSync("userId")
	})
	//寻物数据查询
	const getMyFindOrClaim = async () => {
		let res = await getMyFindOrClaimApi(parm)
		if (res && res.code == 200) {
			console.log(res)
			//设置表格数据
			orderList.value = orderList.value.concat(res.data.records)
			//设置总页数
			pages.value = res.data.pages
			if (res.data.records.length == 0) {
				loadStatus.value = "nomore"
			} else {
				loadStatus.value = "loadmore"
			}
		}
	}
	//加载更多点击事件
	const loadmore = () => {
		console.log('点击加载更多')
		if (parm.currentPage >= pages.value) {
			loadStatus.value = "nomore"
			return;
		}
		loadStatus.value = "loading"
		parm.currentPage = ++parm.currentPage;
		getMyFindOrClaim()
	}
	//触底加载
	onReachBottom(() => {
		console.log('触底加载更多')
		if (parm.currentPage >= pages.value) {
			loadStatus.value = "nomore"
			return;
		}
		loadStatus.value = "loading"
		parm.currentPage = ++parm.currentPage;
		getMyFindOrClaim()
	})
	//删除id
	const goodsId = ref('')
	//删除点击事件
	const deleteBtn = (item)=>{
		goodsId.value = item.goodsId;
		show.value = true;
	}
	//删除确定
	const confirmBtn = async()=>{
		let res = await deleteApi({
			goodsId:goodsId.value
		})
		if(res && res.code == 200){
			orderList.value = [];
			parm.currentPage = 1
			getMyFindOrClaim()
			show.value = false;
		}
		
	}
	//取消删除
	const cancelBtn = ()=>{
		show.value = false;
	}
	//寻回点击事件
	const findId = ref('')
	const findBtn = (item)=>{
		findId.value = item.goodsId;
		findshow.value = true;
	}
	//删除确定
	const findconfirmBtn = async()=>{
		let res = await hasClaimandFindApi({
			goodsId:findId.value
		})
		if(res && res.code == 200){
			orderList.value = [];
			parm.currentPage = 1
			getMyFindOrClaim()
			findshow.value = false;
		}
		
	}
	//取消删除
	const findcancelBtn = ()=>{
		findshow.value = false;
	}
	//查看
	const lookBtn = (item)=>{
		uni.navigateTo({
			url:'../look_find/look_find?goods='+JSON.stringify(item)
		})
	}
	//编辑
	const editBtn = (item)=>{
		//跳转到编辑页面
		uni.navigateTo({
			url:'../find_edit/find_edit?goods='+JSON.stringify(item)
		})
	}
	onReady(() => {
		getMyFindOrClaim()
	})
</script>

<style lang="scss">
	.conBtn{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-right: 15px;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>